package com.source.config;

import com.source.interceptor.AuthorizationInterceptor;
import com.source.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @Author ZZF
 * @Time 2021/03/03 1:23
 * 原文链接：https://blog.csdn.net/cjbfzxz/article/details/106699276
 * 解决前后端分离的跨域问题
 * 拦截器header跨域问题
 * 参考 https://blog.csdn.net/u012534163/article/details/79584694
 */
@Configuration
public class MyMvcConfig {
    @Resource
    private LoginInterceptor loginInterceptor;
    @Resource
    private AuthorizationInterceptor authorizationInterceptor;

        @Bean
        public WebMvcConfigurer webMvcConfigurer() {
            return new WebMvcConfigurer() {

                //配置跨域
                @Override
                public void addCorsMappings(CorsRegistry registry) {
                    registry.addMapping("/**")     //允许的路径
                            .allowedMethods("*")     //允许的方法
                            .allowedOriginPatterns("*")       //允许的网站
                            .allowedHeaders("*")     //允许的请求头
                            .allowCredentials(true)
                            .maxAge(3600);
                }

//                //注册jwt登录认证拦截器和权限拦截器
//                @Override
//                public void addInterceptors(InterceptorRegistry registry) {
//                    //登录拦截
//                    registry.addInterceptor(loginInterceptor)
//                            .addPathPatterns("/**")//拦截所有请求和静态资源
//                            .excludePathPatterns("/error/**")
//                            .excludePathPatterns("/swagger-ui/**","/doc.html/**")//放行swagger-ui
//                            .excludePathPatterns("/swagger-resources/**")
//                            .excludePathPatterns("/v3/**")//放行v3文档
//                            .excludePathPatterns("classpath:/META-INF/**","/webjars/**","/asserts/**")//放行静态资源
//                            .excludePathPatterns("/login","/register","/check/**","/email/**","/get-back-password");//放行登录注册
//                    //权限拦截
//                    registry.addInterceptor(authorizationInterceptor)
//                            .addPathPatterns("/**")//拦截所有请求和静态资源
//                            .excludePathPatterns("/error/**")
//                            .excludePathPatterns("/swagger-ui/**","/doc.html/**")//放行swagger-ui
//                            .excludePathPatterns("/swagger-resources/**")
//                            .excludePathPatterns("/v3/**")//放行v3文档
//                            .excludePathPatterns("classpath:/META-INF/**","/webjars/**","/asserts/**")//放行静态资源
//                            .excludePathPatterns("/login","/register","/check/**","/email/**","/get-back-password")//放行登录注册
//                            .excludePathPatterns("/saving/**");
//                }



//                @Override
//                public void addResourceHandlers(ResourceHandlerRegistry registry) {
//                    registry.addResourceHandler("swagger-ui.html")
//                            .addResourceLocations("classpath:/META-INF/resources/");
//                    registry.addResourceHandler("/webjars")
//                            .addResourceLocations("classpath:/META-INF/resources/webjars/");
//                }
            };
        }

    @Bean
    public MultipartResolver multipartResolver() {
        return new StandardServletMultipartResolver();
    }
}
